package cn.jaye.fabric.application;

import cn.jaye.fabric.application.command.CreateAccessPointCommand;
import cn.jaye.fabric.application.command.EnableStateCommand;
import cn.jaye.fabric.common.constant.Global;
import cn.jaye.fabric.common.constant.MicroConstant;
import cn.jaye.fabric.common.exception.ServiceException;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import cn.jaye.fabric.common.constant.ExceptionEnum;
import cn.jaye.fabric.domain.service.RestTemplateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Jaye
 * @create 2017-12-21 15:45
 */
@Service
public class AccessPointApplicationService {

    @Autowired
    private RestTemplateService restTemplateService;

    @Autowired
    private MicroConstant microConstant;


    private static final String ACCESSPOINT_SERVICE_NAME = "accesspoint";

    private static final String PIPELINE_ACCESSPOINT_SERVICE_NAME = "pipe-accesspoint";


    public String listAccessPoint() {
        String url = microConstant.getApiGatewayAddress() + ACCESSPOINT_SERVICE_NAME + "/accesspoint/list";
        JSONObject postData = new JSONObject();
        postData.put("accessPointUserName", "%");
        JSONObject resultObject = restTemplateService.getUrlResponse(url, postData);
        if (Global.SUCCESS_FLAG.equals(resultObject.getInteger("code"))) {
            return resultObject.getString("data");
        } else {
            throw new ServiceException(ExceptionEnum.ACCESSPOINT_LIST_FAILED);
        }
    }

    public String createAccessPoint(CreateAccessPointCommand createAccessPointCommand) {
        String url = microConstant.getApiGatewayAddress() + PIPELINE_ACCESSPOINT_SERVICE_NAME + "/accesspoint/generate";
        JSONObject postData = new JSONObject();
        postData.put("accessPointUserName", createAccessPointCommand.getAccesspoint_username());
        postData.put("accessPointNickName", createAccessPointCommand.getAccesspoint_nickname());
        JSONObject resultObject = restTemplateService.getUrlResponse(url, postData);
        return JSON.toJSONString(resultObject);
    }

    public String enableAccessPoint(EnableStateCommand enableStateCommand) {
        String url = microConstant.getApiGatewayAddress() + ACCESSPOINT_SERVICE_NAME + "/accesspoint/enable";
        JSONObject postData = new JSONObject();
        postData.put("accessPointId", enableStateCommand.getAccessPointId());
        JSONObject resultObject = restTemplateService.getUrlResponse(url, postData);
        return JSON.toJSONString(resultObject);
    }

    public String disableAccessPoint(EnableStateCommand enableStateCommand) {
        String url = microConstant.getApiGatewayAddress() + ACCESSPOINT_SERVICE_NAME + "/accesspoint/disable";
        JSONObject postData = new JSONObject();
        postData.put("accessPointId", enableStateCommand.getAccessPointId());
        JSONObject resultObject = restTemplateService.getUrlResponse(url, postData);
        return JSON.toJSONString(resultObject);
    }

    public String getSignKey(String accessPointId) {
        String url = microConstant.getApiGatewayAddress() + ACCESSPOINT_SERVICE_NAME + "/secretkey/sign/get";
        JSONObject postData = new JSONObject();
        postData.put("accessPointId", accessPointId);
        postData.put("encryptType", 2);
        JSONObject resultObject = restTemplateService.getUrlResponse(url, postData);
        if (Global.SUCCESS_FLAG.equals(resultObject.getInteger("code"))) {
            return JSON.toJSONString(resultObject);
        } else {
            throw new ServiceException(ExceptionEnum.GET_SIGNSECRETKEY_FAIL);
        }
    }

    public String getParaKey(String accessPointId) {
        String url = microConstant.getApiGatewayAddress() + ACCESSPOINT_SERVICE_NAME + "/secretkey/para/get";
        JSONObject postData = new JSONObject();
        postData.put("accessPointId", accessPointId);
        postData.put("encryptType", 1);
        JSONObject resultObject = restTemplateService.getUrlResponse(url, postData);
        if (Global.SUCCESS_FLAG.equals(resultObject.getInteger("code"))) {
            return resultObject.getString("data");
        } else {
            throw new ServiceException(ExceptionEnum.GET_PARASECRETKEY_FAIL);
        }
    }
}
